package com.mall.server.controller;

import com.mall.commons.R;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.server.service.ProductCategoryService;
import com.tanhua.model.domain.PmsProductCategory;
import com.tanhua.model.dto.ProductCategoryDto;
import com.tanhua.model.vo.PageVo;
import com.tanhua.model.vo.ProductCategoryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/productCategory")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;


    /**
     * 查询所有一级分类及子分类
     * @return
     */
    @GetMapping("/list/withChildren")
    public R listWithChildren(){
        List<ProductCategoryVo> productCategoryVos=productCategoryService.listWithChildren();
        return R.success(productCategoryVos);
    }

    /**
     * 分页查询商品分类
     * @param parentId  必须存在
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list/{parentId}")
    public R findByParentId(@PathVariable("parentId")Integer parentId,Integer pageNum,Integer pageSize){

        if (parentId == null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        PageVo pageVo =productCategoryService.findByParentId(parentId,pageNum,pageSize);
        return R.success(pageVo);
    }

    /**
     * 修改导航栏显示状态
     * @param ids   必须有值
     * @param navStatus 必须有值   0不显示  1显示
     * @return
     */
    @PostMapping("/update/navStatus")
    public R updateNavStatus(Integer[] ids,Integer navStatus){

        if (ids == null  || navStatus ==null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        productCategoryService.updateNavStatus(ids,navStatus);
        return R.success("");
    }

    /**
     * 修改显示状态
     * @param ids   必须有值
     * @param showStatus 必须有值    0不显示  1显示
     * @return
     */
    @PostMapping("/update/showStatus")
    public R updateShowStatus(Integer[] ids,Integer showStatus){

        if (ids == null  || showStatus ==null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        productCategoryService.updateShowStatus(ids,showStatus);
        return R.success("");
    }
    /**
     * 添加商品分类
     * @param productCategoryDto
     * @return
     **/
    @PostMapping("/create")
    public R create(@RequestBody ProductCategoryDto productCategoryDto){

        log.info("==========================================================={}",productCategoryDto);

        productCategoryService.create(productCategoryDto);
        return R.success("");
    }

    /**
     * 根据id获取商品分类
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable("id")Long id){
        PmsProductCategory pmsProductCategory =productCategoryService.getById(id);

        return R.success(pmsProductCategory);
    }


    /**
     * 修改商品分类
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    public R updateById(@PathVariable("id") Long id,@RequestBody ProductCategoryDto productCategoryDto){
        productCategoryService.updateById(id,productCategoryDto);
        return R.success("");
    }

    /**
     * 删除商品分类
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public R  delete(@PathVariable("id") Long id){
        productCategoryService.delete(id);

        return R.success("");
    }

}
